
import { _decorator, Component,Animation, Node, SpriteFrame, AnimationClip } from 'cc';
import { FSM_PARAM_TYPE_ENUM, PARAMS_NAME_ENUM } from '../Enum';
import State from './State';
import { SubStateMechine } from './SubStateMechine';
const { ccclass, property } = _decorator;

type ParamsValueType = boolean | number;

export interface IParamsValue{
    type:FSM_PARAM_TYPE_ENUM,
    value:ParamsValueType
}

export const getInitParamsTrigger = ()=>{
    return{
            type:FSM_PARAM_TYPE_ENUM.TRIGGER,
            value:false
        }
}

export const getInitParamNumber= ()=>{
    return{
            type:FSM_PARAM_TYPE_ENUM.NUMBER,
            value:false
        }
}

@ccclass('StateMechine')
export abstract class StateMechine extends Component {
    private _currenState:State |SubStateMechine = null
    params:Map<string,IParamsValue> = new Map();
    stateMachines:Map<string,State | SubStateMechine> = new Map();
    animationComponent:Animation
    waitingList:Array<Promise<SpriteFrame[]>> = [];



    get currenState(){
        return this._currenState;

    }

    set currenState(newState :State |SubStateMechine){
        this._currenState = newState;
        this._currenState.run()
    }

    getParams(paramsName:string){
        if(this.params.has(paramsName)){
            return this.params.get(paramsName).value
        }
    }

    setParams(paramsName:string,value:ParamsValueType){
        if(this.params.has(paramsName)){
            this.params.get(paramsName).value=value;
            this.run();
            this.resetTrigger();
        }
    }

    resetTrigger(){
        for(const [_,value] of this.params){
            if(value.type === FSM_PARAM_TYPE_ENUM.TRIGGER){
                value.value = false;
            }
        }
    }




   abstract init():void;


   abstract run();

}


